{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Exception in Tkinter callback\n",
      "Traceback (most recent call last):\n",
      "  File \"c:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\tkinter\\__init__.py\", line 1967, in __call__\n",
      "    return self.func(*args)\n",
      "           ^^^^^^^^^^^^^^^^\n",
      "  File \"C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_3276\\2845090533.py\", line 58, in buttonCaptureClick\n",
      "    w = MyCapture(filename)\n",
      "        ^^^^^^^^^^^^^^^^^^^\n",
      "  File \"C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_3276\\2845090533.py\", line 25, in __init__\n",
      "    self.top.geometry('%dx%d+0+0' % (screenWidth, screenHeight), bg='white')\n",
      "TypeError: Wm.wm_geometry() got an unexpected keyword argument 'bg'\n"
     ]
    }
   ],
   "source": [
    "import tkinter as tk\n",
    "from tkinter import filedialog, messagebox\n",
    "import os\n",
    "from PIL import ImageGrab, Image\n",
    "from time import sleep\n",
    "\n",
    "# 创建tkinter应用程序，设置窗口大小和位置，不允许窗口改变大小\n",
    "root = tk.Tk()\n",
    "root.geometry('100x40+400+300')\n",
    "root.resizable(False, False)\n",
    "\n",
    "class MyCapture:\n",
    "    def __init__(self, png):\n",
    "        # 变量X和Y用来记录鼠标左键按下的位置\n",
    "        self.X = tk.IntVar(value=0)\n",
    "        self.Y = tk.IntVar(value=0)\n",
    "\n",
    "        # 屏幕尺寸\n",
    "        screenWidth = root.winfo_screenwidth()\n",
    "        screenHeight = root.winfo_screenheight()\n",
    "\n",
    "        # 创建顶级组件容器\n",
    "        self.top = tk.Toplevel(root)\n",
    "        self.top.overrideredirect(True)\n",
    "        self.top.geometry('%dx%d+0+0' % (screenWidth, screenHeight), bg='white')\n",
    "\n",
    "        # 显示全屏截图，在全屏截图上进行区域截图\n",
    "        self.image = tk.PhotoImage(file=png)\n",
    "        self.canvas = tk.Canvas(self.top, width=screenWidth, height=screenHeight)\n",
    "        self.canvas.pack(fill=tk.BOTH, expand=tk.YES)\n",
    "        self.canvas.create_image(screenWidth // 2, screenHeight // 2, image=self.image)\n",
    "\n",
    "        # 获取鼠标左键抬起的位置，取色，并转换成合适的颜色格式进行显示\n",
    "        def onLeftButtonUp(event):\n",
    "            color = self.canvas.getcolor(event.x, event.y)\n",
    "            msg = str(color) + '\\n'\n",
    "            color = '#' + ''.join(map(lambda x: hex(x)[2:].upper(), color))\n",
    "            msg += color\n",
    "            messagebox.showinfo(\"Color\", msg)\n",
    "\n",
    "        # 关闭当前窗口\n",
    "        def close_window():\n",
    "            self.top.destroy()\n",
    "\n",
    "        self.canvas.bind(\"<ButtonRelease-1>\", onLeftButtonUp)\n",
    "        self.canvas.bind(\"<ButtonRelease-3>\", close_window)\n",
    "\n",
    "# 开始截图\n",
    "def buttonCaptureClick():\n",
    "    # 最小化主窗口\n",
    "    root.state('withdrawn')\n",
    "    filename = 'temp.png'\n",
    "    im = ImageGrab.grab()\n",
    "    im.save(filename)\n",
    "    im.close()\n",
    "\n",
    "    # 显示全屏幕截图\n",
    "    w = MyCapture(filename)\n",
    "    w.top.deiconify()\n",
    "\n",
    "    # 截图结束，恢复主窗口，并删除临时的全屏幕截图文件\n",
    "    root.state('normal')\n",
    "    os.remove(filename)\n",
    "\n",
    "# 创建按钮\n",
    "buttonCapture = tk.Button(root, text='取色', command=buttonCaptureClick)\n",
    "buttonCapture.place(x=10, y=10, width=80, height=20)\n",
    "\n",
    "# 启动消息主循环\n",
    "root.mainloop()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting pillow\n",
      "  Downloading pillow-11.0.0-cp311-cp311-win_amd64.whl.metadata (9.3 kB)\n",
      "Downloading pillow-11.0.0-cp311-cp311-win_amd64.whl (2.6 MB)\n",
      "   ---------------------------------------- 0.0/2.6 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/2.6 MB ? eta -:--:--\n",
      "   ---------------------------------------- 0.0/2.6 MB 445.2 kB/s eta 0:00:06\n",
      "   ---------------------------------------- 0.0/2.6 MB 445.2 kB/s eta 0:00:06\n",
      "    --------------------------------------- 0.0/2.6 MB 219.4 kB/s eta 0:00:12\n",
      "    --------------------------------------- 0.1/2.6 MB 299.4 kB/s eta 0:00:09\n",
      "   - -------------------------------------- 0.1/2.6 MB 409.6 kB/s eta 0:00:06\n",
      "   - -------------------------------------- 0.1/2.6 MB 409.6 kB/s eta 0:00:06\n",
      "   -- ------------------------------------- 0.2/2.6 MB 447.8 kB/s eta 0:00:06\n",
      "   --- ------------------------------------ 0.2/2.6 MB 491.5 kB/s eta 0:00:05\n",
      "   ---- ----------------------------------- 0.3/2.6 MB 608.1 kB/s eta 0:00:04\n",
      "   ---- ----------------------------------- 0.3/2.6 MB 632.7 kB/s eta 0:00:04\n",
      "   ----- ---------------------------------- 0.3/2.6 MB 656.2 kB/s eta 0:00:04\n",
      "   ------- -------------------------------- 0.5/2.6 MB 801.7 kB/s eta 0:00:03\n",
      "   -------- ------------------------------- 0.5/2.6 MB 874.4 kB/s eta 0:00:03\n",
      "   -------- ------------------------------- 0.6/2.6 MB 842.9 kB/s eta 0:00:03\n",
      "   ---------- ----------------------------- 0.7/2.6 MB 917.7 kB/s eta 0:00:03\n",
      "   ----------- ---------------------------- 0.8/2.6 MB 977.1 kB/s eta 0:00:02\n",
      "   ------------ --------------------------- 0.8/2.6 MB 1.0 MB/s eta 0:00:02\n",
      "   ------------- -------------------------- 0.8/2.6 MB 983.9 kB/s eta 0:00:02\n",
      "   -------------- ------------------------- 1.0/2.6 MB 1.1 MB/s eta 0:00:02\n",
      "   ------------------- -------------------- 1.2/2.6 MB 1.3 MB/s eta 0:00:02\n",
      "   -------------------- ------------------- 1.3/2.6 MB 1.4 MB/s eta 0:00:01\n",
      "   ----------------------- ---------------- 1.5/2.6 MB 1.4 MB/s eta 0:00:01\n",
      "   ---------------------------- ----------- 1.8/2.6 MB 1.7 MB/s eta 0:00:01\n",
      "   ------------------------------------ --- 2.3/2.6 MB 2.1 MB/s eta 0:00:01\n",
      "   ---------------------------------------  2.5/2.6 MB 2.2 MB/s eta 0:00:01\n",
      "   ---------------------------------------- 2.6/2.6 MB 2.1 MB/s eta 0:00:00\n",
      "Installing collected packages: pillow\n",
      "Successfully installed pillow-11.0.0\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "[notice] A new release of pip is available: 24.0 -> 24.3.1\n",
      "[notice] To update, run: python.exe -m pip install --upgrade pip\n"
     ]
    }
   ],
   "source": [
    "pip install pillow\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
